package com.liliudong.pinkyponk.biz.sys.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liliudong.pinkyponk.biz.sys.entity.SysMenu;
import com.liliudong.pinkyponk.biz.sys.enums.SysMenuMenuType;
import com.liliudong.pinkyponk.biz.sys.mapper.SysMenuMapper;
import com.liliudong.pinkyponk.biz.sys.service.SysMenuService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * SysMenuService
 *
 * @author liliudong
 * @date 2022-06-01 16:20
 **/
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements
        SysMenuService {

    @Override
    public List<String> getAuthoritiesByRoleIds(List<Integer> roleIds) {
        return baseMapper.selectAuthoritiesByRoleIds(roleIds);
    }

    @Override
    public List<String> getAllAuthorities() {
        return baseMapper.selectAllAuthorities();
    }

    @Override
    public List<SysMenu> listExcludeButton() {
        return list(Wrappers.<SysMenu>lambdaQuery().ne(SysMenu::getMenuType, SysMenuMenuType.BUTTON));
    }

    @Override
    public List<Tree<Integer>> listTreeExcludeButton() {
        List<SysMenu> sysMenus = listExcludeButton();
        return translateTree(sysMenus);
    }

    @Override
    public List<Tree<Integer>> treeByUserIdIgnoreDataScope(Long userId) {
        List<SysMenu> sysMenus = baseMapper.selectMenuListByUserIdIgnoreDataScope(userId);
        return translateTree(sysMenus);
    }

    private List<Tree<Integer>> translateTree(List<SysMenu> sysMenus) {
        return TreeUtil.build(sysMenus, 1, (treeNode, tree) -> {
            tree.setId(treeNode.getId());
            tree.setParentId(treeNode.getParentId());
            tree.setName(treeNode.getMenuName());
            tree.putExtra("menuName", treeNode.getMenuName());
            tree.putExtra("menuIcon", treeNode.getMenuIcon());
            tree.putExtra("routeName", treeNode.getRouteName());
            tree.putExtra("menuType", treeNode.getMenuType());
            tree.putExtra("menuOrder", treeNode.getMenuOrder());
        });
    }
}

